function log(logMessage: string) {
  browser.runtime.sendMessage({ log: '这是一条来自background的日志消息' });
}

export default defineBackground(() => {
  console.log('Hello background!',{ id: browser.runtime.id });
  log('2222')
  browser.runtime.onMessage.addListener(function (request,sender,sendResponse) {
    log('text')
    if (request.action === "downloadFile") {
      console.log(request)
      browser.downloads.download({
        url: request.url,
        filename: "_tmp/downloaded_file.jpg",
        saveAs: false,
        conflictAction: 'overwrite',
      }).then((downloadId) => {
        browser.downloads.onChanged.addListener((delta) => { 
          if (delta.id === downloadId && delta.state && delta.state.current === 'complete') {
            console.log('Download completed')
          }
        })
        sendResponse({ downloadId })
      });
    }
    return true
  });
});
